Conditional (computer programming)
part 12/26 · 46.2 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
In the above example the condition is not evaluated before calling the function. Instead, the implementation of the if function receives the condition as a string value and is responsible to evaluate this string as an expression in the callers scope.cite-ref-7[7]
Such a behavior is possible by using uplevel and expr commands:
Uplevel makes it possible to implement new control constructs as Tcl procedures (for example, uplevel could be used to implement the while construct as a Tcl procedure).cite-ref-8[8]
Because if is actually a function it also returns a value:
The return value from the command is the result of the body script that was executed, or an empty string if none of the expressions was non-zero and there was no bodyN.cite-ref-9[9]
Rust
In Rust, if is always an expression. It evaluates to the value of whichever branch is executed, or to the unit type () if no branch is executed. If a branch does not provide a return value, it evaluates to () by default. To ensure the if expression's type is known at compile time, each branch must evaluate to a value of the same type. For this reason, an else branch is effectively compulsory unless the other branches evaluate to (), because an if without an else can always evaluate to () by default.cite-ref-10[10]
// Assign my_variable some value, depending on the value of x
let my_variable = if x > 20 {
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────